package org.bulu.project.web.controller.video;

import org.bulu.project.common.result.Result;
import org.bulu.project.system.model.dto.VideoSreachDto;
import org.bulu.project.system.model.entity.SysUser;
import org.bulu.project.system.model.entity.Video;
import org.bulu.project.system.model.entity.VideoStats;
import org.bulu.project.system.model.vo.VideoVo;
import org.bulu.project.system.service.SysUserService;
import org.bulu.project.system.service.VideoSearchService;
import org.bulu.project.system.service.VideoService;
import org.bulu.project.system.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/videoSearch")
public class VideoSearchController {
    @Autowired
    private VideoSearchService videoSearchService;
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private VideoService videoService;

    @Autowired
    private SysUserService sysUserService;

    @GetMapping("/search")
    public Result search() {
        try {
            Integer count = 8;
            Set<Object> idSet = redisUtil.srandmember("video_status:0", count);
            List<VideoVo> videoVos = videoSearchService.search(idSet);
            return Result.success(videoVos);
        } catch (Exception e) {
            return Result.failed(e.getMessage());
        }
    }

    /***
     * 返回所有视频
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findAllVideo")
    public Result findAllVideo(@RequestParam("pageNum") Integer pageNum,
                               @RequestParam("pageSize") Integer pageSize){
        try{
            VideoSreachDto videoSreachDto = new VideoSreachDto();
            videoSreachDto.setPageNum(pageNum);
            videoSreachDto.setPageSize(pageSize);
            return Result.success(videoSearchService.findAllVideo(videoSreachDto));
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("获取失败");
        }
    }

    /***
     * 根据分类查找视频
     * @param mcId 主分区，不为null
     * @param scId 子分区
     * @return
     */
    @GetMapping("/findVideoByCategory")
    public Result findVideoByCategory(@RequestParam("mcId") String mcId,
                                      @RequestParam("scId") String scId,
                                      @RequestParam("pageNum") Integer pageNum,
                                      @RequestParam("pageSize") Integer pageSize){
        try{
            VideoSreachDto videoSreachDto = new VideoSreachDto();
            videoSreachDto.setMcId(mcId);
            videoSreachDto.setScId(scId);
            videoSreachDto.setPageNum(pageNum);
            videoSreachDto.setPageSize(pageSize);
            return Result.success(videoSearchService.findVideoByCategory(videoSreachDto));
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("获取失败");
        }
    }

    /***
     * 更据标题查询
     * @param title
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/findVideoByTitle")
    public Result findVideoByTitle(@RequestParam("title") String title,
                                   @RequestParam("type") Integer type,
                                   @RequestParam("pageNum") Integer pageNum,
                                   @RequestParam("pageSize") Integer pageSize){
        try{
            VideoSreachDto videoSreachDto = new VideoSreachDto();
            if(type == 0){
                videoSreachDto.setTitle(title);
                videoSreachDto.setPageNum(pageNum);
                videoSreachDto.setPageSize(pageSize);
                return Result.success(videoSearchService.findVideoByTitle(videoSreachDto));
            }
            if(type == 1){
                videoSreachDto.setVid(Long.valueOf(title));
                videoSreachDto.setPageNum(pageNum);
                videoSreachDto.setPageSize(pageSize);
                return Result.success(videoSearchService.findVideoByVideoId(videoSreachDto));
            }
            if(type == 2){
                videoSreachDto.setUid(Long.valueOf(title));
                videoSreachDto.setPageNum(pageNum);
                videoSreachDto.setPageSize(pageSize);
                return Result.success(videoSearchService.findVideoByUserId(videoSreachDto));
            }
            if(type == 3){
                videoSreachDto.setUserName(title);
                videoSreachDto.setPageNum(pageNum);
                videoSreachDto.setPageSize(pageSize);
                return Result.success(videoSearchService.findVideoByUserName(videoSreachDto));
            }
            if(type == 4){
                videoSreachDto.setMcName(title);
                videoSreachDto.setScName(title);
                videoSreachDto.setPageNum(pageNum);
                videoSreachDto.setPageSize(pageSize);
                return Result.success(videoSearchService.findVideoByCategory(videoSreachDto));
            }
            return Result.failed("获取失败");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failed("获取失败");
        }
    }

    /***
     * 根据视频id查找视频
     * @param uid
     * @return
     */
    @GetMapping("/findVideoById")
    public Result findVideoByUserId(@RequestParam("uid") Long uid){
        try{
            return Result.success(videoSearchService.findVideoById(uid));
        }catch (Exception e){
            return Result.failed("获取失败");
        }

    }

    /**
     * 根据VID获取视频信息*
     */
    @GetMapping("/getVideoByVid")
    public Result getVideoByVid(@RequestParam("vid") Integer vid){
        Video video = videoService.getVideoWithDataById(vid);
        if (video != null){
            System.out.println(video);
            return Result.success(video);
        }else{
            return Result.failed("视频不存在！！！");
        }
    }

    /**
     * 根据VID查询视频Status
     */
    @GetMapping("/getVideoStats")
    public Result getVideoStats(@RequestParam("vid") Integer vid){
        VideoStats videoStats = videoService.getVideoStats(vid);
        if (videoStats == null){
            return Result.failed("视频不存在！！！");
        }else {
            return Result.success(videoStats);
        }
    }

    /**
     * 通过uid获取UP主信息
     */
    @GetMapping("/getUserInfo")
    public Result getUserInfo(@RequestParam("uid") Integer uid){
        SysUser sysUser = sysUserService.getUserById(uid);
        if (sysUser != null){
            return Result.success(sysUser);
        }else{
            return Result.failed("用户不存在！！！");
        }
    }
}
